// import 'dart:isolate';

import 'package:flutter/material.dart';

// 将list数组的布局平均分配布局
extension ExpandEqually on Iterable<Widget> {
  Iterable<Widget> expandedEqually() => map(
        (w) => Expanded(
          flex: 1,
          child: w,
        ),
      );
}

extension MapToList<T> on Iterable<T> {
  List<E> mapList<E>(E Function(T) toElement) => map(toElement).toList();
}

extension Flatten on Iterable<bool> {
  bool flatten() => fold(
        true,
        (lhs, rhs) => lhs && rhs,
      );
}

void testIt() {
  assert([true, false, true].flatten() == false);
  assert([true, true, true].flatten() == true);
  assert([false, false, false].flatten() == false);
  assert([true].flatten() == true);
  assert([false].flatten() == false);
}

extension MapIfOfType<T> on T {
  R? mapIfOfType<E, R>(R Function(E) f) {
    final self = this;
    if (self is E) {
      return f(self);
    } else {
      return null;
    }
  }
}

// 使用示例
// Row(
//   mainAxisSize: MainAxisSize.max,
//   children: [
//     Container(height: 200, color: Colors.yellow),
//     Container(height: 200, color: Colors.blue),
//   ].expandedEqually().toList(),
// );

// 异步消息
// Stream<String> getMessages() {
//   final rp = ReceivePort();
//   return Isolate.spawn(_getMessages, rp.sendPort)
//       .asStream()
//       .asyncExpand((_) => rp)
//       .takeWhile((element) => element is String)
//       .cast();
// }

// void _getMessages(SendPort sp) async {
//   await for (final now in Stream.periodic(
//     const Duration(seconds: 1),
//     (_) => DateTime.now().toIso8601String(),
//   ).take(10)) {
//     sp.send(now);
//   }
//   Isolate.exit(sp);
// }

// void testIt() async {
//   await for (final msg in getMessages()) {
//     print(msg);
//   }
// }
